<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="钓场名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入钓场名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="queryParams.address" placeholder="请输入地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="省份" prop="province">
              <el-input v-model="queryParams.province" placeholder="请输入省份" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="城市" prop="city">
              <el-input v-model="queryParams.city" placeholder="请输入城市" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="经度" prop="latitude">
              <el-input v-model="queryParams.latitude" placeholder="请输入经度" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="纬度" prop="longitude">
              <el-input v-model="queryParams.longitude" placeholder="请输入纬度" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="收费类型" prop="feeType">
              <el-select v-model="queryParams.feeType" placeholder="请选择收费类型" clearable >
                <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="收费金额" prop="amount">
              <el-input v-model="queryParams.amount" placeholder="请输入收费金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input v-model="queryParams.description" placeholder="请输入描述" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否公开" prop="publicFlag">
              <el-input v-model="queryParams.publicFlag" placeholder="请输入是否公开; Y 公开，N 不公开" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户id" prop="userId">
              <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['fishSite:fishSite:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['fishSite:fishSite:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['fishSite:fishSite:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['fishSite:fishSite:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="fishSiteList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="fishSiteId" v-if="true" />
        <el-table-column label="钓场名称" align="center" prop="name" />
        <el-table-column label="地址" align="center" prop="address" />
        <el-table-column label="省份" align="center" prop="province" />
        <el-table-column label="城市" align="center" prop="city" />
        <el-table-column label="经度" align="center" prop="latitude" />
        <el-table-column label="纬度" align="center" prop="longitude" />
        <el-table-column label="收费类型" align="center" prop="feeType">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.feeType"/>
          </template>
        </el-table-column>
        <el-table-column label="收费金额" align="center" prop="amount" />
        <el-table-column label="描述" align="center" prop="description" />
        <el-table-column label="图片url" align="center" prop="imageUrl" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="是否公开; Y 公开，N 不公开" align="center" prop="publicFlag" />
        <el-table-column label="用户id" align="center" prop="userId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fishSite:fishSite:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fishSite:fishSite:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改钓场对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="fishSiteFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="钓场名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入钓场名称" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="经度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="收费类型" prop="feeType">
          <el-select v-model="form.feeType" placeholder="请选择收费类型">
            <el-option
                v-for="dict in sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入收费金额" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="图片url" prop="imageUrl">
          <image-upload v-model="form.imageUrl"/>
        </el-form-item>
        <el-form-item label="是否公开; Y 公开，N 不公开" prop="publicFlag">
          <el-input v-model="form.publicFlag" placeholder="请输入是否公开; Y 公开，N 不公开" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FishSite" lang="ts">
import { listFishSite, getFishSite, delFishSite, addFishSite, updateFishSite } from '@/api/fishSite/fishSite';
import { FishSiteVO, FishSiteQuery, FishSiteForm } from '@/api/fishSite/fishSite/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_yes_no } = toRefs<any>(proxy?.useDict('sys_yes_no'));

const fishSiteList = ref<FishSiteVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const fishSiteFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: FishSiteForm = {
  fishSiteId: undefined,
  name: undefined,
  address: undefined,
  province: undefined,
  city: undefined,
  latitude: undefined,
  longitude: undefined,
  feeType: undefined,
  amount: undefined,
  description: undefined,
  imageUrl: undefined,
  publicFlag: undefined,
  userId: undefined,
}
const data = reactive<PageData<FishSiteForm, FishSiteQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    address: undefined,
    province: undefined,
    city: undefined,
    latitude: undefined,
    longitude: undefined,
    feeType: undefined,
    amount: undefined,
    description: undefined,
    imageUrl: undefined,
    publicFlag: undefined,
    userId: undefined,
    params: {
    }
  },
  rules: {
    fishSiteId: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "钓场名称不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    province: [
      { required: true, message: "省份不能为空", trigger: "blur" }
    ],
    city: [
      { required: true, message: "城市不能为空", trigger: "blur" }
    ],
    latitude: [
      { required: true, message: "经度不能为空", trigger: "blur" }
    ],
    longitude: [
      { required: true, message: "纬度不能为空", trigger: "blur" }
    ],
    feeType: [
      { required: true, message: "收费类型不能为空", trigger: "change" }
    ],
    amount: [
      { required: true, message: "收费金额不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "描述不能为空", trigger: "blur" }
    ],
    imageUrl: [
      { required: true, message: "图片url不能为空", trigger: "blur" }
    ],
    publicFlag: [
      { required: true, message: "是否公开; Y 公开，N 不公开不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询钓场列表 */
const getList = async () => {
  loading.value = true;
  const res = await listFishSite(queryParams.value);
  fishSiteList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  fishSiteFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: FishSiteVO[]) => {
  ids.value = selection.map(item => item.fishSiteId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加钓场";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: FishSiteVO) => {
  reset();
  const _fishSiteId = row?.fishSiteId || ids.value[0]
  const res = await getFishSite(_fishSiteId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改钓场";
}

/** 提交按钮 */
const submitForm = () => {
  fishSiteFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.fishSiteId) {
        await updateFishSite(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addFishSite(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: FishSiteVO) => {
  const _fishSiteIds = row?.fishSiteId || ids.value;
  await proxy?.$modal.confirm('是否确认删除钓场编号为"' + _fishSiteIds + '"的数据项？').finally(() => loading.value = false);
  await delFishSite(_fishSiteIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('fishSite/fishSite/export', {
    ...queryParams.value
  }, `fishSite_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
